class ResetAlertView extends egret.DisplayObjectContainer {

    private _bgMask: egret.Shape; // 背景幕布
    private _contentView: egret.DisplayObjectContainer; // contentView
    private _bg: egret.Bitmap; // 背景
    private _bg2: egret.Bitmap; // 内部背景
    private _sureBtn: egret.DisplayObjectContainer; // 按钮
    private _titleLabel: BaseTextField; // 标题
    private _textLabel: BaseTextField; // 文本
    private _cancleBtn: TbitMap;
    private _videoSureBtn: egret.DisplayObjectContainer; // 视频确定按钮
    private _shareSureBtn: egret.DisplayObjectContainer; // 分享确定按钮
    public type: number = 1; // 重置类型 1是种子重置 2是分享重置 3是看视频重置

    private successCb: () => {};
    private cancleCb: () => {};


    constructor() {
        super();
        this.height = GameData.stageH;
        this.width = GameData.stageW;
        this.touchEnabled = true;
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.viewDidLoad, this);
    }

    private viewDidLoad() {
        // 1.幕布
        if (!this.contains(this.bgMask)) this.addChild(this.bgMask);
        // 2.contentView
        if (!this.contains(this.contentView)) this.addChild(this.contentView);
        // 3.bg
        if (!this.contentView.contains(this.bg)) this.contentView.addChild(this.bg);
        // 4.内部背景
        if (!this.contentView.contains(this.bg2)) this.contentView.addChild(this.bg2);
        // 5.内容
        if (!this.contentView.contains(this.textLabel)) this.contentView.addChild(this.textLabel);
        // 7. 大标题
        if (!this.contentView.contains(this.titleLabel)) this.contentView.addChild(this.titleLabel);
        // 8.X
        if (!this.contentView.contains(this.cancleBtn)) { this.contentView.addChild(this.cancleBtn); }

        // 根据类型 添加按钮
        this.addSureBtn();


        // 动画
        this.addAnimation();
    }

    private addSureBtn() {
        switch (this.type) {
            case 1:
                this.getTypeBtn(this.sureBtn);
                break;
            case 2:
                this.getTypeBtn(this.shareSureBtn);
                break;
            case 3:
                this.getTypeBtn(this.videoSureBtn);
                break;
        }
    }

    private getTypeBtn(obj:egret.DisplayObjectContainer){
        if(this.contentView.contains(this.sureBtn)) this.contentView.removeChild(this.sureBtn);
        if(this.contentView.contains(this.videoSureBtn)) this.contentView.removeChild(this.videoSureBtn);
        if(this.contentView.contains(this.shareSureBtn)) this.contentView.removeChild(this.shareSureBtn);
        this.contentView.addChild(obj);
    }


    public setCallBack(success, cancle): void {
        this.successCb = success;
        this.cancleCb = cancle;
    }


    /*********************************************** private **********************************************************/
    private tapSureAction() {
        if (this.successCb) {
            this.successCb();
        }
    }

    private removeSelf() {
        if (this.cancleCb) {
            this.cancleCb();
        }
    }

    private addAnimation() {
        this.contentView.scaleX = 0;
        this.contentView.scaleY = 0;
        egret.Tween.removeTweens(this.contentView);
        egret.Tween.get(this.contentView).to({ scaleX: 1.0, scaleY: 1.0 }, 500, egret.Ease.backOut);
    }

    /*********************************************** getter **********************************************************/


    private get textLabel(): BaseTextField {
        if (!this._textLabel) {
            var textLabel = new BaseTextField();
            textLabel.textColor = 0x8F5B12;
            textLabel.lineSpacing = 15;
            textLabel.width = this.bg2.width;
            textLabel.bold = true;
            textLabel.x = this.bg2.x;
            textLabel.text = '您的次数已经用完了\n打的不爽？';
            textLabel.y = this.bg2.y + 80;
            textLabel.textAlign = egret.HorizontalAlign.CENTER;
            textLabel.size = 36;
            this._textLabel = textLabel;
        }
        return this._textLabel;
    }


    private get titleLabel(): BaseTextField {
        if (!this._titleLabel) {
            var titleLabel = new BaseTextField();
            titleLabel.textColor = 0xFFEC79;
            titleLabel.text = "提示";
            titleLabel.stroke = 2;
            titleLabel.width = this.contentView.width;
            titleLabel.y = 40;
            titleLabel.textAlign = egret.HorizontalAlign.CENTER;
            titleLabel.size = 50;
            titleLabel.bold = true;
            titleLabel.strokeColor = 0x6E3E0E;
            this._titleLabel = titleLabel;
        }
        return this._titleLabel;
    }

    private get sureBtn(): egret.DisplayObjectContainer {
        if (!this._sureBtn) {

            var btn_container = new egret.DisplayObjectContainer();
            var sureBtn = utils.createBitmapByName('btn_set');
            btn_container.addChild(sureBtn);

            var seed = utils.createBitmapByName('seed');
            var seedNum = utils.createTextField({
                text: '' + GameData.resetSeed,
                textColor: 0xFBE005,
                stroke: 2,
                strokeColor: 0x8F5B12,
                size: 30,
                height: btn_container.height,
            });
            var reset = utils.createTextField({
                text: '重打一次',
                textColor: 0xFFFFFF,
                stroke: 2,
                strokeColor: 0x8F5B12,
                size: 28,
                height: btn_container.height,
            });
            seed.x = btn_container.width / 2 - (seed.width + seedNum.width + reset.width + 20) / 2;
            seed.y = btn_container.height / 2 - seed.height / 2;
            seedNum.x = seed.x + seed.width + 10;
            reset.x = seedNum.x + seedNum.width + 10;
            btn_container.addChild(seed);
            btn_container.addChild(seedNum);
            btn_container.addChild(reset);

            btn_container.anchorOffsetX = btn_container.width / 2;
            btn_container.anchorOffsetY = btn_container.height / 2;
            btn_container.x = this.contentView.width / 2;
            btn_container.y = this.bg2.y + this.bg2.height - btn_container.height / 2 - 30;
            btn_container.touchEnabled = true;
            btn_container.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tapSureAction, this);
            this._sureBtn = btn_container;
        }
        return this._sureBtn;
    }

    private get videoSureBtn(): egret.DisplayObjectContainer {
        if (!this._videoSureBtn) {
            var btn_container = new egret.DisplayObjectContainer();
            var sureBtn = utils.createBitmapByName('btn_set');
            btn_container.addChild(sureBtn);

            var player = utils.createBitmapByName('player_icon');
            var reset = utils.createTextField({
                text: '重打一次',
                textColor: 0xFFFFFF,
                stroke: 2,
                strokeColor: 0x8F5B12,
                size: 28,
                height: btn_container.height - 4,
            });
            player.x = btn_container.width / 2 - (player.width + reset.width + 20) / 2;
            player.y = btn_container.height / 2 - player.height / 2 - 4;
            reset.x = player.x + player.width + 20;
            btn_container.addChild(player);
            btn_container.addChild(reset);

            btn_container.anchorOffsetX = btn_container.width / 2;
            btn_container.anchorOffsetY = btn_container.height / 2;
            btn_container.x = this.contentView.width / 2;
            btn_container.y = this.bg2.y + this.bg2.height - btn_container.height / 2 - 30;
            btn_container.touchEnabled = true;
            btn_container.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tapSureAction, this);
            this._videoSureBtn = btn_container;
        }
        return this._videoSureBtn;
    }

    private get shareSureBtn(): egret.DisplayObjectContainer {
        if (!this._shareSureBtn) {
            var btn_container = new egret.DisplayObjectContainer();
            var sureBtn = utils.createBitmapByName('btn_set_g');
            btn_container.addChild(sureBtn);

            var reset = utils.createTextField({
                text: '分享重打一次',
                textColor: 0xFFFFFF,
                stroke: 2,
                strokeColor: 0x284E00,
                size: 28,
                height: btn_container.height - 4,
            });
            reset.width = btn_container.width;
            btn_container.addChild(reset);

            btn_container.anchorOffsetX = btn_container.width / 2;
            btn_container.anchorOffsetY = btn_container.height / 2;
            btn_container.x = this.contentView.width / 2;
            btn_container.y = this.bg2.y + this.bg2.height - btn_container.height / 2 - 30;
            btn_container.touchEnabled = true;
            btn_container.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tapSureAction, this);
            this._shareSureBtn = btn_container;
        }
        return this._shareSureBtn;
    }

    private get bg2(): egret.Bitmap {
        if (!this._bg2) {
            var bg2 = utils.createBitmapByName('notice_bg', { width: this.contentView.width - 2 * 20 });
            bg2.x = 60;
            bg2.y = 140;
            bg2.scale9Grid = new egret.Rectangle(80, 80, 380, 120);
            bg2.height = this.contentView.height - 240;
            bg2.width = (this.contentView.width - 2 * bg2.x);
            this._bg2 = bg2;
        }
        return this._bg2;
    }

    private get bg(): egret.Bitmap {
        if (!this._bg) {
            var bg = utils.createBitmapByName('friend', { width: this.contentView.width });
            bg.scale9Grid = new egret.Rectangle(0, 146, 600, 91);
            bg.height = this.contentView.height;
            this._bg = bg;
        }
        return this._bg;
    }

    private get contentView(): egret.DisplayObjectContainer {
        if (!this._contentView) {
            var contentView = new egret.DisplayObjectContainer();
            contentView.touchEnabled = true;
            contentView.width = this.width * 0.95;
            contentView.height = 640;
            contentView.anchorOffsetX = contentView.width / 2.0;
            contentView.anchorOffsetY = contentView.height / 2.0;
            contentView.x = this.width / 2.0;
            contentView.y = this.height / 2.0;
            this._contentView = contentView;
        }
        return this._contentView;
    }

    private get bgMask(): egret.Shape {
        if (!this._bgMask) {
            var bgMask = new egret.Shape();
            bgMask.graphics.beginFill(0x000000, 0.7);
            bgMask.graphics.drawRect(0, 0, this.width, this.height);
            bgMask.graphics.endFill();
            this._bgMask = bgMask;
        }
        return this._bgMask;
    }

    private get cancleBtn(): TbitMap {
        if (!this._cancleBtn) {
            var btn = utils.createBitmapByName('x', { offsetX: 0.5, offsetY: 0.5 });
            btn.x = this.contentView.width - 40;
            btn.y = 60;
            btn.touchAnimation = true;
            btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.removeSelf, this);
            this._cancleBtn = btn;
        }

        return this._cancleBtn;
    }

}